package com.lsc.www.day04;

import java.util.Scanner;

public class DichotomyTest {
    public static void main(String[] args) {
        //二分法查找
        //数据必须从小到大排序
        int[] arr = new int[]{99,98,70,60,100,10,80};
        for (int i = 0 ;  i<arr.length -1 ; i++){
//            System.out.println("第"+(i+1)+"轮");
//            System.out.println(arr[i]);
            for (int j = 0 ; j < arr.length - 1 - i; j++){
//                System.out.println(arr[j]+ "和" +arr[j+1]+"比较");
                if(arr[j]>arr[j+1]){//arr[j] = 3 arr[j+1] = 1
                    int temp = arr[j]; // temp = 3 =  arr[j] = 3
                    arr[j]=arr[j+1];// arr[j] = 1 = arr[j+1] = 1
                    arr[j+1] = temp; //  arr[j] = 3 = tmep = 3
                }
            }
        }
        for(int i = 0 ; i <arr.length ; i++){
            System.out.println(arr[i]);
        }
        Scanner sc = new Scanner(System.in);

        //普通查找数组中的数据
//        for (int i = 0 ; i <arr.length ; i++){
//            if (arr[i] == num){
//                System.out.println(arr[i]);
//            }
//        }

        //二分法
        //定义最低下标
        int low =0 ;
        //定义最高下标
        int hight = arr.length-1;
        //用户输入
        //{10,60,70,80,98,99,100}0+7=7

        System.out.println("输入要查找的数据");
        int num = sc.nextInt();

        while (low<=hight){
            //7/2
            int mid = (low+hight)/2;
            //3
            System.out.println(mid);
            if(num == arr[mid]){
                System.out.println(arr[mid]);
                break;
            }
            if(num > arr[mid]){
                low = mid+1;
            }
            if(num < arr[mid]){
                hight = mid - 1;
            }
        }
    }
}
